package com.winz.cultureapp.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.application.DemoApplication;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.LoginUserBean;
import com.winz.cultureapp.entity.UserSendRes;
import com.winz.cultureapp.entity.request.AdminUserLoginReq;
import com.winz.cultureapp.entity.request.UserLoginReq;
import com.winz.cultureapp.entity.request.UserRegisterReq;
import com.winz.cultureapp.entity.request.UserSendReq;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.CountDownManager;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by terry on 2017/10/30.
 * 登录界面
 */

public class LoginActivity extends BaseAppCompatActivity {
    @BindView(R.id.et_phone)
    EditText mEt_phone;
    @BindView(R.id.et_code)
    EditText mEt_code;
    @BindView(R.id.tv_title)
    TextView mTv_title;
    @BindView(R.id.tv_code)
    TextView mTv_code;
    @BindView(R.id.tv_register)
    TextView mTv_register;
    @BindView(R.id.btn_login)
    Button mBtn_login;
    @BindView(R.id.cb_admin)
    CheckBox mCb_admin;

    private boolean isLogin = true; // true为登录状态，false为注册状态
    private CountDownManager count;

    private boolean isAdmin = false;

    @Override
    protected int contentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews() {
        count = new CountDownManager();
    }

    @Override
    protected void initEvents() {
        count.setOnFinishedListener(new CountDownManager.OnFinishedListener() {
            @Override
            public void onFinished() {
                mTv_code.setClickable(true);
                mTv_code.setText("重新发送");
            }
        });

        mCb_admin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isAdmin = b;
            }
        });

    }

    public boolean isAllRight() {
        if (TextUtils.isEmpty(getEditTextStr(mEt_phone))) {
            showMsg("请输入手机号码");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_code))) {
            showMsg("请输入验证码");
            return false;
        }
        return true;
    }

    /**
     * 注册
     */
    private void register() {
        showProgressDialog();
        HttpUitls.onPost("user_register", new UserRegisterReq(getEditTextStr(mEt_phone),getEditTextStr(mEt_code)), new OnResponeListener<LoginUserBean>() {
            @Override
            public void onSuccess(LoginUserBean bean) {
                Logger.d("user_register==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    showMsg("注册成功");
                    // 保存参数
                    SharePrefsUtil.getInstance().savedUserInfo(bean.data);
                    finish();
                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("user_register==>>", error);
                dismissProgressDialog();
            }
        });
    }

    /**
     * 登录 -- 普通用户
     */
    private void login() {
        showProgressDialog();
        if (isAdmin){
            HttpUitls.onPost("admin_user_login", new AdminUserLoginReq(getEditTextStr(mEt_phone),getEditTextStr(mEt_code)), new OnResponeListener<LoginUserBean>() {
                @Override
                public void onSuccess(LoginUserBean bean) {
                    Logger.d("admin_user_login==>>", JsonUtil.getJsonFromObj(bean));
                    if (bean.code == 0) {
                        showMsg("登录成功");
                        // 保存参数
                        SharePrefsUtil.getInstance().savedUserInfo(bean.data);
                        SharePrefsUtil.getInstance().putBoolean(Constants.USER_INFO.ISADMIN,true);
                        DemoApplication.getInstance().isAdmin = true;



                        finish();
                    }else{
                        showMsg(bean.msg);
                    }
                    dismissProgressDialog();
                }

                @Override
                public void onFailed(String error) {
                    Logger.d("admin_user_login==>>", error);
                    dismissProgressDialog();
                }
            });
        }else {
            HttpUitls.onPost("user_login", new UserLoginReq(getEditTextStr(mEt_phone),getEditTextStr(mEt_code)), new OnResponeListener<LoginUserBean>() {
                @Override
                public void onSuccess(LoginUserBean bean) {
                    Logger.d("user_login==>>", JsonUtil.getJsonFromObj(bean));
                    if (bean.code == 0) {
                        showMsg("登录成功");
                        // 保存参数
                        SharePrefsUtil.getInstance().savedUserInfo(bean.data);
                        SharePrefsUtil.getInstance().putBoolean(Constants.USER_INFO.ISADMIN,false);
                        DemoApplication.getInstance().isAdmin = false;
                        finish();
                    }else{
                        showMsg(bean.msg);
                    }
                    dismissProgressDialog();
                }

                @Override
                public void onFailed(String error) {
                    Logger.d("user_login==>>", error);
                    dismissProgressDialog();
                }
            });
        }


    }

    /**
     * 调用验证码
     */
    private void
    sendCode(String type) {
        HttpUitls.onPost("user_send", new UserSendReq(mEt_phone.getText().toString(),type) ,new OnResponeListener<UserSendRes>() {
            @Override
            public void onSuccess(UserSendRes bean) {
                Logger.d("user_send==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    showMsg("验证码发送成功");
                }else{
                    showMsg(bean.msg);

                }
            }

            @Override
            public void onFailed(String error) {
                Logger.d("user_send==>>", error);
            }
        });
    }

    @OnClick({R.id.iv_close, R.id.btn_login, R.id.tv_register,R.id.tv_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.btn_login:
                if (isAllRight()){
                    if (isLogin){
                        // 登录
                        login();
                    }else{
                        if (isAdmin){
                            showMsg("暂不能在APP上注册管理员帐号");
                        }else {
                            // 注册
                            register();
                        }
                    }
                }
                break;
            case R.id.tv_register:
                isLogin = !isLogin;
                changeState();
                break;
            case R.id.tv_code:
                sendCode(isLogin?"1":"0");
                count.startCount(mTv_code,60);
                break;
        }
    }

    /**
     * 改变状态
     */
    private void changeState() {
        if (isLogin){
            mEt_code.setText("");
            mEt_phone.setText("");
            mTv_title.setText("登录茂名文化云");
            mTv_register.setText("账号注册");
            mBtn_login.setText("登录");
        }else {
            mEt_code.setText("");
            mEt_phone.setText("");
            mTv_title.setText("注册茂名文化云");
            mTv_register.setText("立即登录");
            mBtn_login.setText("注册");
        }
    }
}
